<script type="text/javascript">
    window.history.forward(1);
</script>
<a href="/"><div class="topBack"><img src="/images/back_jiantou.png" style="width:40px;height:40px;"></div></a>
<div class="topContent">商品详情</div>
<div class="topRight">&nbsp;</div>
<div class="clear"></div>
<!--添加微信-->
<!--威信-->
<input type="hidden" id="appId" value="<?php echo $this->wxDate['appId']?>">
<input type="hidden" id="timestamp" value="<?php echo $this->wxDate['timestamp']?>">
<input type="hidden" id="nonceStr" value="<?php echo $this->wxDate['nonceStr']?>">
<input type="hidden" id="signature" value="<?php echo $this->wxDate['signature']?>">
<input type="hidden" id="url" value="<?php echo $this->wxDate['url']?>">
<input type="hidden" id="rawString" value="<?php echo $this->wxDate['rawString']?>">

<input type="hidden" id="share_title" value="<?php echo  $this->wxDate['title']?>">
<input type="hidden" id="share_image" value="<?php echo  $this->wxDate['image']?>">
<input type="hidden" id="share_content" value="<?php echo  $this->wxDate['desc']?>">
<!---->
<?php
if(empty($this->res)){
    echo '
		<div style="text-align:center;"><img src="/images/none.jpg" style="width:50%;padding-top:110px;"></div>
		<div style="text-align:center;">暂无商品详情</div>';
}else{
    echo '
			<div id="wrapperDetails" class="wrapperComman" style="top:40px;">
			<div class="scroller">
				<div style="text-align:center;padding:20px 10px;">
					<img src="'.$this->res->imgurl.'" style="width:100%;">
				</div>
				<div class="normalDiv"><span class="skuD_title">'.$this->res->skuname.'</span></div>
				<div class="normalDiv">';
					if($this->res->onsale==1){
						echo '￥<span style="font-size:1.1em;color:red;">'.$this->res->saleprice.'</span> ￥<span style="text-decoration: line-through;">'.$this->res->price.'</span>';
					}else{
						echo '￥<span style="font-size:1.1em;color:red;">'.$this->res->price.'</span>';
					}
					echo '
		<span style="background-color:#EB1412;color:white;padding:5px;font-size:0.8em;">返现'.$this->res->cashBack.'元</span>
		<span style="float:right;margin-right:10px;clear:both;font-size:0.9em;">已售'.$this->res->selledNum.'份&nbsp;&nbsp;</span>
		</div>';
	$feature = $this->res->feature;
	if(!empty($feature)){
	echo '<div style="margin-top:10px;background-color:#FFEBEC;color:#E20E0B;padding:5px 10px;font-size:0.9em;">'.$feature.'</div>';
	}
	
	echo '<div class="hrCss"></div>';
	echo '<div style="font-size:0.75em;color:#379C34;padding:10px;">下单成功后分享订单至朋友圈，即可获得一张现金券。</div>';
	echo '<div><img style="width:100%;" src="/images/liuchenghui.png"></div>';
	echo '<div class="hrCss"></div>';
		
	echo '<div id="productIMG"></div>';	
    if(!empty($this->res->imageList)){
        foreach($this->res->imageList as $i){
            echo '<input type="hidden" class="imgUrls" value="'.$i->imgurl.'">';
        }
    }
    echo '</div></div>
	
<div class="index_bottom" style="border:1px solid red;">
	<div style="max-width:640px;margin:0px auto;">
		<div class="active_choose">
			<div style="float: left;width: 30%;text-align: center;font-size: 0.95em;">数量</div>
			<div style="float: left;width: 69%;">
				<div style="float: left;width: 33%;text-align: center;">
					<img src="/images/min.png" id="min" onclick="javascript:presaleReduce();return false;" height="32px" width="32px">
				</div>
				<div style="float: left;width: 32%;text-align: center;">
					<span id="product_num" style="font-size: 1.30em;color: #EB1515;">1</span>
				</div>
				<div style="float: left;width: 33%;">
					<img src="/images/add.png" id="add" onclick="javascript:presaleAdd();return false;" height="32px" width="32px">
				</div>
			</div>
		</div>
		<span class="active_a" id="presale_sub">立刻购买</span>
	</div>
</div>
		';
}
?>
<input type="hidden" id="shopid" value="<?php echo $this->res->shopid ?>"/>

<script type="text/javascript">
    var myscroll;
    function loaded(){
		loadCookiePreSkuDetail();
		//show imgs after dom ready 
		$('.imgUrls').each(function(index,el){
			var imgUrl = $(el).val();
			$('#productIMG').append('<img src="'+imgUrl+'" class="imgs" style="width:100%;">');
		});
		//check img has loaded
		var imgdefereds=[];
		$('img').each(function(){
			 var dfd=$.Deferred();
			 $(this).bind('load',function(){
				dfd.resolve();
			 }).bind('error',function(){
			 })
			 if(this.complete) setTimeout(function(){
				dfd.resolve();
			 },1000);
			 imgdefereds.push(dfd);
		})
		$.when.apply(null,imgdefereds).done(function(){
			setTimeout(function(){
				myscroll=new iScroll("wrapperDetails");
			},100 );
		});
			
    }
    window.addEventListener("load",loaded,false);
	
	
    function presaleAdd() {
        presale_info.num = presale_info.num*1 + 1;
        sessionStorage.setItem('presku',JSON.stringify(presale_info));
        $('#product_num').text(presale_info.num);
    }

    function presaleReduce() {
        if (presale_info.num == 1) {
            alert('亲，至少选一个哦！');
            return false;
        }
        presale_info.num = presale_info.num - 1;
        sessionStorage.setItem('presku',JSON.stringify(presale_info));
        $('#product_num').text(presale_info.num);
    }

    function loadCookiePreSkuDetail(){
        var presku = sessionStorage.getItem('presku');
        if (presku != null) {
            var obj = JSON.parse(presku);
            if (obj.sku_id == <?php echo $this->res->skuid ?>) {
                $('#product_num').text(obj.num);
            }
        }
        presale_info = {'sku_id':<?php echo  $this->res->skuid?>,'num':$('#product_num').text()};
        sessionStorage.setItem('presku',JSON.stringify(presale_info));
    }
    var page_vistit_timp = Date.parse(new Date());
    $('#presale_sub').click(function(){
        $(this).attr('disabled',true);
        if (Date.parse(new Date()) - page_vistit_timp > 300000) {
            location.href = location.href;
            return false;
        }

        var shopid= $('#shopid').val();
        var presku = sessionStorage.getItem('presku');
        if (presku == null) {
            alert('亲，还未选择商品哦！');
            $(this).attr('disabled',false);
            return false;
        } else {
            var obj = JSON.parse(presku);
            var product_id = parseInt(obj.sku_id);
            var product_num = parseInt(obj.num);
        }

        if (isNaN(product_id) || isNaN(product_num)) {
            alert('亲，还未选择商品哦！');
            $(this).attr('disabled',false);
            return false;
        }

        var postBody = "shopid="+shopid+"&product_id="+product_id+"&product_num="+product_num;
        $.ajax({
            type: "post",
            url : "/presalesku",
            dataType: 'text',
            data: postBody,
            success: function(info){
                if(info=='error'){
                    alert('结算失败');
                    $(this).attr('disabled',false);
                    return;
                }
                if(info==0){
                    location.href="/login?rurl=presalesku/details?id=<?php echo $this->res->id ?>";
                }
                if(info>0){
                    location.href="/order/complete?type=advance&oid="+info;
                }
            }
        });
    });
/*****************微信 2016.5.23 by 赵鑫****************/
    var appId = $('#appId').val();
    var timestamp = $('#timestamp').val();
    var nonceStr = $('#nonceStr').val();
    var signature = $('#signature').val();
    var url = $('#url').val();
    var rawString = $('#rawString').val();
    /*分享内容*/
    var share_title = $('#share_title').val();
    var share_image = $('#share_image').val();
    var share_content = $('#share_content').val();

    //加载分享js
    wx.config({
        debug:false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
        appId: appId, // 必填，公众号的唯一标识
        timestamp: parseInt(timestamp), // 必填，生成签名的时间戳
        nonceStr: nonceStr, // 必填，生成签名的随机串
        signature: signature,// 必填，签名，见附录1
        jsApiList: ["onMenuShareTimeline","onMenuShareAppMessage","showMenuItems","onMenuShareQQ","onMenuShareQZone"] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
    });

    /*分享到朋友圈*/
    wx.ready(function(){
        //分享到朋友圈
        wx.onMenuShareTimeline({
            title:share_title, // 分享标题
            link:url, // 分享链接
            imgUrl: share_image, // 分享图标
            success: function (res) {
                // 用户确认分享后执行的回调函数
                //alert(res.errMsg);
                if(res.errMsg== 'shareTimeline:ok'){
                   // location.reload();
                }
            },
            cancel: function (res) {
                // 用户取消分享后执行的回调函数
                //alert(res.errMsg);
                //location.reload()
            }
        });
        //分享给朋友
        wx.onMenuShareAppMessage({
            title:share_title, // 分享标题
            desc:share_content, // 分享标题
            link: url, // 分享链接
            imgUrl:share_image, // 分享图标
            type: '', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function (res) {
                //用户确认分享后执行的函数
                //alert(res.errMsg);
                if(res.errMsg=="sendAppMessage:ok"){
                    //location.reload();
                    // alert('分享成功');
                }
            },
            cancel: function (res) {
                // 用户取消分享后执行的回调函数
                // alert(res.errMsg);
                if(res.errMsg=="sendAppMessage:cancel"){
                    //location.reload();
                }
            }
        });
        //分享到qq
        wx.onMenuShareQQ({
            title:share_title, // 分享标题
            desc: share_content, // 分享描述
            link: url, // 分享链接
            imgUrl: share_image, // 分享图标
            success: function () {
                // 用户确认分享后执行的回调函数
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
            }
        });
        //分享到qq空间
        wx.onMenuShareQZone({
            title:share_title, // 分享标题
            desc:share_content, // 分享描述
            link:url, // 分享链接
            imgUrl:share_image, // 分享图标
            success: function () {
                // 用户确认分享后执行的回调函数
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
            }
        });



    });
</script>
